package cn.edu.sjtu.se.dslab.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.data.DataAttribute;
import cn.edu.sjtu.se.dslab.component.data.DataType;
import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.service.IDataAttributeService;
import cn.edu.sjtu.se.dslab.service.IDataTypeService;
import cn.edu.sjtu.se.dslab.service.IDomainEntityService;
import cn.edu.sjtu.se.dslab.util.Const;

@Controller
@RequestMapping(value = "/domain_entity/type")
public class DataTypeController {
	@Resource(name = "dataTypeService")
	IDataTypeService dataTypeService;
	
	@Resource(name = "domainEntityService")
	IDomainEntityService domainEntityService;
	
	@Resource(name="dataAttributeService")
	IDataAttributeService dataAttributeService;
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView getDataTypeList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@CookieValue(value = "domain_type", defaultValue = "type") String domainType,HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		model.setViewName("domain_entity");
		try {
			Cookie foo = new Cookie("domain_type", "type"); //bake cookie	
			response.addCookie(foo); //put cookie in response
			PageBean<DataType> bean = dataTypeService.getPageBean(pageNum);
			model.addObject("bean", bean);
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return model;
	}
	
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addDataType() {
		ModelAndView model = new ModelAndView();
		model.setViewName("add_data_type");
		try {
			List<DataType> _typeList = dataTypeService.getDataTypeList();
			List<DomainEntity> typeList = domainEntityService.getAllDomainEntity();
			typeList.addAll(_typeList);
			model.addObject("typeList", typeList);
			return model;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return model;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addDataType(DataType dataType,@RequestParam int type_flag,
			@RequestParam(required = false) String[] enums,
			@RequestParam(required = false) String[] attrs,
			@RequestParam(required = false) long[] types,@RequestParam(required = false) int[] attr_length) {
		logger.info("添加 DataType:" + new Date().toString()
				+ "----start");
		if (type_flag == 1) {  //enum类型
			 dataType.setCategory(Const.DATA_TYPE_ENUM);
			 dataType.setType(Const.DATA_TYPE);
			 if(enums != null){
				 StringBuffer listBuffer = new StringBuffer();
				 for (int i = 0; i < enums.length; i++) {
						listBuffer.append(enums[i]+";");//枚举项以；分割
				 }
				 dataType.setEnumList(listBuffer.toString());
			 }
			 
			 dataTypeService.addDataType(dataType);
		}else {    //struct类型
			dataType.setCategory(Const.DATA_TYPE_STRUCT);
			 dataType.setType(Const.DATA_TYPE);
			 dataTypeService.addDataType(dataType);
			 if (types != null && attrs != null && types.length == attrs.length) {
				for (int i = 0; i < types.length; i++) {
					DataAttribute attribute = new DataAttribute();
					attribute.setEntity(dataType);
					attribute.setName(attrs[i]);
					attribute.setLength(attr_length[i]);
					attribute.setOrder(i+1);
					DomainEntity type = domainEntityService.getDomainEntityById(types[i]);
					attribute.setType(type);
					dataAttributeService.addDataAttribute(attribute);
				}
			}
		}
		logger.info("添加 DataType:" + new Date().toString()
				+ "----end");
		return "redirect:/domain_entity/type";
	}
}
